[WPF] The calling thread cannot access this object because a different thread owns it.

Posted by zunyite on Stack Overflow See other posts from Stack Overflow or by zunyite
Published on 2010-04-28T11:16:47Z Indexed on 2010/04/28 11:33 UTC
Read the original article Hit count: 270

Filed under:
|

Why I can't create CroppedBitmap in the following code ? I got an exception : The calling thread cannot access this object because a different thread owns it.

public MainWindow()
{
    InitializeComponent();

    ThreadPool.QueueUserWorkItem((o) =>
        {
            //load a large image file
            var bf = BitmapFrame.Create(
                new Uri("D:\\1172735642.jpg"),
                BitmapCreateOptions.DelayCreation | BitmapCreateOptions.IgnoreColorProfile,
                BitmapCacheOption.None);
            bf.Freeze();
            Dispatcher.BeginInvoke(
                new Action(() =>
                    {
                        CroppedBitmap cb = new CroppedBitmap(bf, new Int32Rect(1,1,5,5));
                        cb.Freeze();
                        //set Image's source to cb....
                    }), 
                    System.Windows.Threading.DispatcherPriority.ApplicationIdle);
         }
    );
}

© Stack Overflow or respective owner

Related posts about wpf

Related posts about thread